home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld 1998 January
/
Macworld (1998-01).dmg
/
Shareware World
/
Comms & Internet
/
HTML mode 2.0 etc.
/
cssMode.tcl
next >
Wrap
Text File
|
1997-09-22
|
13KB
|
423 lines
## -*-Tcl-*-
# ###################################################################
# CSS mode - tools for editing CSS documents
#
# FILE: "cssMode.tcl"
# created: 97-03-01 17.02.41
# last update: 97-09-20 19.27.54
# Author: Johan Linde
# E-mail: <jl@theophys.kth.se>
# www: <http://bach.theophys.kth.se/~jl/Alpha.html>
#
# Version: 1.0
#
# Copyright 1997 by Johan Linde
#
# This software may be used freely, and distributed freely, as long as the
# receiver is not obligated in any way by receiving it.
#
# If you make improvements to this file, please share them!
#
# ###################################################################
##
# Defining CSS mode
if {$startingUp} {
addMode CSS cssMenu {*.css *.CSS} cssMenu
addMenu cssMenu
set cssMenu CSS
# set cssMenu •150
return
}
proc cssMenu {} {}
# ◊◊◊◊ Change below for new system §9 ◊◊◊◊ #
newModeVar CSS wordWrap 0 1
newModeVar CSS wordBreak {\w+} 0
newModeVar CSS wordBreakPreface {\W} 0
newModeVar CSS commentColor red 0
newModeVar CSS keywordColor blue 0
newModeVar CSS htmlColor magenta 0
newModeVar CSS elecRBrace 1 1
newModeVar CSS elecLBrace 1 1
newModeVar CSS electricSemi 1 1
newModeVar CSS openNonTextFile 1 1
# ◊◊◊◊ end changing for new system §9 ◊◊◊◊ #
# Coloring
proc cssColoring {} {
global CSSmodeVars
regModeKeywords -b {/*} {*/} -c $CSSmodeVars(commentColor) -k $CSSmodeVars(keywordColor) CSS {
font-family font-style font-variant font-weight font-size font
color background-color background-image background-repeat background-attachment
background-position background word-spacing letter-spacing text-decoration
vertical-align text-transform text-align text-indent line-height
margin-top margin-right margin-bottom margin-left margin padding-top padding-right
padding-bottom padding-left padding border-top-width border-right-width
border-bottom-width border-left-width border-width border-color border-style
border-top border-right border-bottom border-left border width height float clear
display white-space list-style-type list-style-image list-style-position list-style
@import important
link visited active first-letter first-line}
regModeKeywords -a -k $CSSmodeVars(htmlColor) CSS [cssGetHtmlWords]
}
# Load other CSS mode files.
foreach tmp {hctsmsl hctsmslShared hctsmslMenu} {
if {[info exists htmlModeIsLoaded] && ($tmp == "hctsmslMenu" || $tmp == "hctsmslShared")} {continue}
if {$tmp == "hctsmsl" && [info exists cssUnits]} {continue}
if { [catch {${tmp}.tcl}] } {
beep
alertnote "Loading of ${tmp}.tcl failed"
return
}
}
# Clean up after version 1.0b1
if {[info exists htmlMenuKey]} {
foreach tmp [array names htmlMenuKey] {
removeArrDef htmlMenuKey $tmp
}
htmlWriteMenuKeys
}
proc cssHTMLelement {elem} {
insertText $elem
}
# ◊◊◊◊ Change below for new system §10 ◊◊◊◊ #
proc CSSindentLine {} {
CindentLine
}
# ◊◊◊◊ end changing for new system §10 ◊◊◊◊ #
proc CSSDividingLine {} {
insertText "/*=============================================================================*/\r"
}
bind 'l' <C> CSSDividingLine CSS
# ◊◊◊◊ Change below for new system §17 ◊◊◊◊ #
proc CSScarriageReturn {} {
HTMLcarriageReturn
}
# ◊◊◊◊ end changing for new system §17 ◊◊◊◊ #
proc CSSelectricSemi {} {
global CSSmodeVars
if {[isSelection]} {deleteSelection}
if {$CSSmodeVars(electricSemi)} {
insertText ";\r"
CindentLine
} else {
insertText ";"
}
}
proc parseFuncsCSS {} {
set pos 0
set funcExpr {^[ \t]*([^\r\{]+)\{}
while {[set res [search -s -f 1 -r 1 -i 0 -n $funcExpr $pos]] != ""} {
if {[regexp $funcExpr [getText [car $res] [cadr $res]] dummy word]} {
lappend m [list $word [car $res]]
}
set pos [cadr $res]
}
return [join [lsort -ignore $m]]
}
#===============================================================================
# Key bindings
#===============================================================================
# Define key bindings from html menu.
proc cssBindingsFromMenu {me tmplist} {
global htmlMenuKey html${me}Sub
upvar $tmplist tmp
foreach it [set html${me}Sub] {
if {[llength $it] > 2} {
set elem [lindex $it 2]
if {[info exists htmlMenuKey(${me}/[lindex $it 1])]} {
set key $htmlMenuKey(${me}/[lindex $it 1])
} else {
set key [lindex $it 0]
}
cssBindOneKey $key $elem "" tmp
}
}
}
proc cssBindOneKey {key elem {un ""} {tmplist ""}} {
set funcKeys {Enter 0x0d 0x09 Clear F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 F13 F14 F15
Help 0x08 Del Home End Pgup Pgdn}
set mods ""
if {[regexp {<B} $key]} {append mods z}
if {[regexp {<I} $key]} {append mods o}
if {[regexp {<U} $key]} {append mods s}
if {[regexp {<O} $key]} {append mods c}
set key1 ""
if {[regexp {/(.)} $key a b]} {
if {[string compare $b a] == -1 || [string compare $b z] == 1} {
set key1 '[string tolower $b]'
} else {
set key1 [lindex $funcKeys [expr [htmlAscii $b] - 97]]
}
}
if {$key1 == ""} {return}
if {[string range $key1 0 1] == "0x"} {
set cmd ${un}ascii
} else {
set cmd ${un}bind
}
eval [list $cmd $key1 <$mods> "cssHTMLelement $elem" CSS]
if {$tmplist != ""} {
upvar $tmplist tmp
append tmp [list $cmd $key1 <$mods> "cssHTMLelement $elem" CSS] \r
}
}
# Redefine key bindings when changed in HTML menu.
proc cssReBindKey {meny keyItems} {
global html${meny}Sub
set items [set html${meny}Sub]
foreach it $keyItems {
set it0 [lindex $it 0]
foreach it1 $items {
if {[lindex $it1 1] == $it0} {
set elem [lindex $it1 2]
break
}
}
# Skip those which aren't html elements
if {[llength $it1] < 3} {continue}
cssBindOneKey [lindex $it 1] $elem un
cssBindOneKey [lindex $it 2] $elem
}
}
proc cssMenuKeys {} {
global htmlMenuKey
set rebuildCSS 0
set somethingModified 0
htmlReadMenuKeys
htmlSetKeysInMenu CSS
if {$somethingModified} {htmlWriteMenuKeys}
catch {unset htmlMenuKey}
if {$rebuildCSS} {cssRebuildMenu}
}
#===============================================================================
# Menu handling
#===============================================================================
set htmlCSSSub [concat [lrange $htmlStyleSub 3 end] {"(-" {"<O<U/S" "Reload in Browser"} {"<O/c" "Word Complete"} {"" "Colors…"} {"" "Key Bindings…"} {"" "Preferences…"}}]
proc cssBuildMenu {} {
global cssMenu htmlMenuKey
if {[catch {htmlReadCache "CSS menu cache" css}]} {
htmlReadMenuKeys
set me [htmlBuildOneMenu CSS]
menu -M CSS -p cssMenuItem -m -n $cssMenu $me
set h {menu -M CSS -p cssMenuItem -m -n $cssMenu}
lappend h $me
htmlSaveCache "CSS menu cache" $h css
}
catch {unset htmlMenuKey}
}
proc cssRebuildMenu {} {
htmlDeleteCache "CSS menu cache"
cssBuildMenu
}
proc cssMenuItem {menu item} {
switch $item {
"Reload in Browser" {cssReloadinBrowser}
"Word Complete" {cssWordComplete}
Colors {htmlColors}
"Key Bindings" {cssMenuKeys}
"Preferences" {CSSmodifyFlags}
Import {cssDialog @import}
Display {cssDialog Display}
default {cssDialog [join [string tolower $item] -]}
}
}
#===============================================================================
# Cmd-double-click
#===============================================================================
proc CSSDblClick {from to} {
HTMLDblClick $from $to
}
#===============================================================================
# Reload in Browser
#===============================================================================
proc cssReloadinBrowser {} {
global browserSig HTMLmodeVars
if {$browserSig != "MOSS" && $browserSig != "MSIE"} {
beep; message "Works only with Netscape and MSIE."; return
}
# returns window ids
if {![regexp {\[([0-9]+)} [AEBuild -r '$browserSig' WWW! LSTW] dum winnum]} {beep; message "No browser window."; return}
if {[winDirty]} {
if {[set ask [askyesno -c "Save '[lindex [winNames] 0]'?"]] == "yes"} {
save
} elseif {$ask == "cancel"} {
return
}
}
# returns window info
regexp {\[([^ ]+)} [AEBuild -r '$browserSig' WWW! WNFO ---- $winnum] dum winurl
set winurl [string trim $winurl "“”,"]
if {$winurl == "'TEXT'()"} {beep; message "Empty browser window."; return}
# reloads window
set flgs ""
if {$browserSig == "MSIE"} {set flgs "FLGS 1"}
eval AEBuild '$browserSig' WWW! OURL ---- "“$winurl”" $flgs
if {![info exists HTMLmodeVars(browseInForeground)] || $HTMLmodeVars(browseInForeground)} {switchTo '$browserSig'}
}
#===============================================================================
# Preferences
#===============================================================================
proc CSSmodifyFlags {} {
global CSSmodeVars modifiedModeVars colorInds
set colors {none blue cyan green magenta red white yellow}
foreach c [lsort [array names colorInds]] {
if {[regexp {color} $c]} {lappend colors $c}
}
set box "-t {CSS mode Preferences} 100 10 450 30 \
-c {Electric left braces} $CSSmodeVars(elecLBrace) 10 35 450 50 \
-c {Electric right braces} $CSSmodeVars(elecRBrace) 10 55 450 70 \
-c {Electric semicolon} $CSSmodeVars(electricSemi) 10 75 450 90 \
-c {Cmd-double-clicking on non-text file link opens file} $CSSmodeVars(openNonTextFile) 10 95 450 110 \
-t {Color of keywords:} 10 115 150 130 \
-m [list [concat $CSSmodeVars(keywordColor) $colors]] 160 115 310 135 \
-t {Color of comments:} 10 140 150 155 \
-m [list [concat $CSSmodeVars(commentColor) $colors]] 160 140 310 155 \
-t {Color of HTML:} 10 165 150 180 \
-m [list [concat $CSSmodeVars(htmlColor) $colors]] 160 165 310 180 \
-t wordBreak: 10 190 150 205 \
-e [list $CSSmodeVars(wordBreak)] 155 190 450 205 \
-t wordBreakPreface: 10 215 150 230 \
-e [list $CSSmodeVars(wordBreakPreface)] 155 215 450 230"
set values [eval [concat dialog -w 460 -h 275 -b OK 20 245 85 265 -b Cancel 110 245 175 265 $box]]
if {[lindex $values 1]} {return}
set i 1
foreach flag {elecLBrace elecRBrace electricSemi openNonTextFile keywordColor
commentColor htmlColor wordBreak wordBreakPreface} {
global $flag
incr i
set val [lindex $values $i]
if {$CSSmodeVars($flag) != $val} {
set $flag $val
set CSSmodeVars($flag) $val
lappend modifiedModeVars [list $flag CSSmodeVars]
if {[string match "*Color" $flag]} {cssColoring}
}
}
}
#===============================================================================
# Initialization
#===============================================================================
set cssVersion 2.0
# Define key bindings.
if {[catch {htmlReadCache "CSS keybindings cache" css}]} {
htmlReadMenuKeys
message "Defining key bindings…"
foreach tmp [lrange $htmlSubMenus $htmlStartElements end] {
cssBindingsFromMenu [lindex $tmp 0] tmplist
}
if {[info exists htmlModeIsLoaded]} {
foreach tmp [array names htmlElemKeyBinding] {
cssBindOneKey $htmlElemKeyBinding($tmp) $tmp "" tmplist
}
} elseif {[file exists $PREFS:HTMLadditions.tcl]} {
set tmpcid [scancontext create]
scanmatch $tmpcid {htmlElemKeyBinding} {
cssBindOneKey [lindex [lindex $matchInfo(line) 1] 2] [lindex $matchInfo(line) 0] "" tmplist
}
if {![catch {open $PREFS:HTMLadditions.tcl} tmpfid]} {
scanfile $tmpcid $tmpfid
close $tmpfid
}
scancontext delete $tmpcid
catch {unset tmpcid tmpfid}
}
htmlSaveCache "CSS keybindings cache" $tmplist css
unset tmplist
catch {unset htmlMenuKey}
} else {
message "Reading key bindings…"
}
if {![info exists htmlModeIsLoaded]} {
set cssHtmlWords {A ADDRESS APPLET AREA B BASE BASEFONT BGSOUND BIG
BLINK BLOCKQUOTE BODY BR
CAPTION CENTER CITE CODE COL COLGROUP DD DFN DIR DIV DL DT EM EMBED FONT
FORM FRAME FRAMESET H1 H2 H3 H4 H5 H6 HEAD HR HTML I IFRAME ILAYER IMG INPUT ISINDEX KBD
KEYGEN LAYER LI LINK MAP MARQUEE MENU META MULTICOL NOBR NOEMBED NOFRAMES NOLAYER NOSCRIPT
OBJECT OL OPTION P PARAM PRE SAMP SCRIPT SELECT SMALL SPACER SPAN STRIKE
STRONG STYLE SUB SUP TABLE TBODY TD TEXTAREA TFOOT TH THEAD TITLE TR TT U
UL VAR WBR}
if {[file exists $PREFS:HTMLadditions.tcl]} {
set tmpcid [scancontext create]
scanmatch $tmpcid {htmlElemKeyBinding} {
lappend cssHtmlWords [lindex $matchInfo(line) 0]
}
if {![catch {open $PREFS:HTMLadditions.tcl} tmpfid]} {
scanfile $tmpcid $tmpfid
close $tmpfid
}
scancontext delete $tmpcid
catch {unset tmpcid tmpfid}
}
}
cssColoring
cssBuildMenu
unset tmp
rename cssBindingsFromMenu ""
# ◊◊◊◊ Change below for new system §11 ◊◊◊◊ #
if {[info commands useUniversalCompletions] == ""} {
# No Vince's Additions
bind '\{' <s> electricLeft CSS
bind '\}' <s> electricRight CSS
bind '\;' CSSelectricSemi CSS
bind '\r' CSScarriageReturn CSS
} else {
# Vince's Additions
set commentCharacters(CSS:General) "/*"
set commentCharacters(CSS:Paragraph) [list "/* " " */" " * "]
set commentCharacters(CSS:Box) [list "/*" 2 "*/" 2 "*" 3]
}
# ◊◊◊◊ end changing for new system §11 ◊◊◊◊ #
if {[info exists htmlModeIsLoaded] && $htmlVersion != $cssVersion} {
alertnote "Warning: The versions of HTML mode and CSS mode may not be compatible.\
Always install new versions of HTML mode and CSS mode simultaneously."
}
set cssModeIsLoaded 1
message "CSS initialization complete."